﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient; 
using System.Text;

public partial class ybqy_1_10 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //先判断是否有数据，如无数据增加3条数据
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        SqlCommand sqlcmd = new SqlCommand("select * from a01110 where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"] + " order by PROJECT_ID", sqlcon);

        SqlDataReader reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);



        if (!IsPostBack)
        {
            int sfys = 0;
            int i = 1;
            while (reader.Read())//有数据，将数据显示
            {
                sfys = 1;

                if (i == 1)
                {
                    PROJECT_NAME1.Text = reader["PROJECT_NAME"].ToString();
                    BEGIN_DATE1.Text = reader["BEGIN_DATE"].ToString();
                    USE_DATE1.Text = reader["USE_DATE"].ToString();
                    PLAN_MONEY1.Text = reader["PLAN_MONEY"].ToString();
                    SUM_YEARMONEY1.Text = reader["SUM_YEARMONEY"].ToString();
                    ALL_MONEY1.Text = reader["ALL_MONEY"].ToString();
                    YUS_MONEY1.Text = reader["YUS_MONEY"].ToString();
                    ENV_MONEY1.Text = reader["ENV_MONEY"].ToString();
                    OTHER_SUMMONEY1.Text = reader["OTHER_SUMMONEY"].ToString();
                    LEND_MONEY1.Text = reader["LEND_MONEY"].ToString();
                    FOREIN_MONEY1.Text = reader["FOREIN_MONEY"].ToString();
                    SELF_MONEY1.Text = reader["SELF_MONEY"].ToString();
                    DESIGN_ABILITY1.Text = reader["DESIGN_ABILITY"].ToString();
                    if (reader["MANAGE_TYPE"].ToString() != null && reader["MANAGE_TYPE"].ToString() != "")
                    {
                        MANAGE_TYPE1.SelectedValue = reader["MANAGE_TYPE"].ToString();
                    }

                }

                if (i == 2)
                {
                    PROJECT_NAME2.Text = reader["PROJECT_NAME"].ToString();
                    BEGIN_DATE2.Text = reader["BEGIN_DATE"].ToString();
                    USE_DATE2.Text = reader["USE_DATE"].ToString();
                    PLAN_MONEY2.Text = reader["PLAN_MONEY"].ToString();
                    SUM_YEARMONEY2.Text = reader["SUM_YEARMONEY"].ToString();
                    ALL_MONEY2.Text = reader["ALL_MONEY"].ToString();
                    YUS_MONEY2.Text = reader["YUS_MONEY"].ToString();
                    ENV_MONEY2.Text = reader["ENV_MONEY"].ToString();
                    OTHER_SUMMONEY2.Text = reader["OTHER_SUMMONEY"].ToString();
                    LEND_MONEY2.Text = reader["LEND_MONEY"].ToString();
                    FOREIN_MONEY2.Text = reader["FOREIN_MONEY"].ToString();
                    SELF_MONEY2.Text = reader["SELF_MONEY"].ToString();
                    DESIGN_ABILITY2.Text = reader["DESIGN_ABILITY"].ToString();
                    if (reader["MANAGE_TYPE"].ToString() != null && reader["MANAGE_TYPE"].ToString() != "")
                    {
                        MANAGE_TYPE2.SelectedValue = reader["MANAGE_TYPE"].ToString();
                    }

                }
                if (i == 3)
                {
                    PROJECT_NAME3.Text = reader["PROJECT_NAME"].ToString();
                    BEGIN_DATE3.Text = reader["BEGIN_DATE"].ToString();
                    USE_DATE3.Text = reader["USE_DATE"].ToString();
                    PLAN_MONEY3.Text = reader["PLAN_MONEY"].ToString();
                    SUM_YEARMONEY3.Text = reader["SUM_YEARMONEY"].ToString();
                    ALL_MONEY3.Text = reader["ALL_MONEY"].ToString();
                    YUS_MONEY3.Text = reader["YUS_MONEY"].ToString();
                    ENV_MONEY3.Text = reader["ENV_MONEY"].ToString();
                    OTHER_SUMMONEY3.Text = reader["OTHER_SUMMONEY"].ToString();
                    LEND_MONEY3.Text = reader["LEND_MONEY"].ToString();
                    FOREIN_MONEY3.Text = reader["FOREIN_MONEY"].ToString();
                    SELF_MONEY3.Text = reader["SELF_MONEY"].ToString();
                    DESIGN_ABILITY3.Text = reader["DESIGN_ABILITY"].ToString();
                    if (reader["MANAGE_TYPE"].ToString() != null && reader["MANAGE_TYPE"].ToString() != "")
                    {
                        MANAGE_TYPE3.SelectedValue = reader["MANAGE_TYPE"].ToString();
                    }

                }
                i = i + 1;
            }
            if (sfys == 0)//无数据，新增数据
            {
                reader.Close();
                sqlcon.Open();

                string sqlstr = @"insert into a01110(qiyeid,ShenBaoNianDu,FACT_ID,DATA_YEAR,PROJECT_ID,jy,PROJECT_SIGN)VALUES("
                + "'" + Session["qiyeid"]
                + "','" + Session["ShenBaoNianDu"]
                + "','1','" + Session["ShenBaoNianDu"]
                + "','00002','weijiaoyan',0)";

                sqlcmd.CommandText = sqlstr;
                sqlcmd.ExecuteNonQuery();

                sqlstr = @"insert into a01110(qiyeid,ShenBaoNianDu,FACT_ID,DATA_YEAR,PROJECT_ID,jy,PROJECT_SIGN)VALUES("
                + "'" + Session["qiyeid"]
                + "','" + Session["ShenBaoNianDu"]
                + "','1','" + Session["ShenBaoNianDu"]
                + "','00003','weijiaoyan',0)";

                sqlcmd.CommandText = sqlstr;
                sqlcmd.ExecuteNonQuery();

                sqlstr = @"insert into a01110(qiyeid,ShenBaoNianDu,FACT_ID,DATA_YEAR,PROJECT_ID,jy,PROJECT_SIGN)VALUES("
                + "'" + Session["qiyeid"]
                + "','" + Session["ShenBaoNianDu"]
                + "','1','" + Session["ShenBaoNianDu"]
                + "','00004','weijiaoyan',0)";

                sqlcmd.CommandText = sqlstr;
                sqlcmd.ExecuteNonQuery();


            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //将数据更新到数据库
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        string sqlstr = @"update a01110 set BEGIN_DATE=null,USE_DATE=null where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];// +
        SqlCommand sqlcmd = new SqlCommand(sqlstr, sqlcon);

        sqlcmd.ExecuteNonQuery();



        sqlstr = @"update a01110 set PROJECT_NAME='" + PROJECT_NAME1.Text + "'," +
           "MANAGE_TYPE='" + MANAGE_TYPE1.SelectedValue + "',";// +
        if (BEGIN_DATE1.Text != "")
        {
            sqlstr = sqlstr + "BEGIN_DATE='" + BEGIN_DATE1.Text + "',";
        }
        if (USE_DATE1.Text != "")
        {
            sqlstr = sqlstr + "USE_DATE='" + USE_DATE1.Text + "',";
        }

        sqlstr = sqlstr + "PLAN_MONEY='" + PLAN_MONEY1.Text + "'," +
            "SUM_YEARMONEY='" + SUM_YEARMONEY1.Text + "'," +
            "ALL_MONEY='" + ALL_MONEY1.Text + "'," +
            "YUS_MONEY='" + YUS_MONEY1.Text + "'," +
            "ENV_MONEY='" + ENV_MONEY1.Text + "'," +
            "OTHER_SUMMONEY='" + OTHER_SUMMONEY1.Text + "'," +
            "LEND_MONEY='" + LEND_MONEY1.Text + "'," +
            "FOREIN_MONEY='" + FOREIN_MONEY1.Text + "'," +
            "SELF_MONEY='" + SELF_MONEY1.Text + "'," +
            "DESIGN_ABILITY='" + DESIGN_ABILITY1.Text + "'" +
            "where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"] + " and PROJECT_ID='00002'";
        sqlcmd.CommandText = sqlstr;

        sqlcmd.ExecuteNonQuery();

        sqlstr = @"update a01110 set PROJECT_NAME='" + PROJECT_NAME2.Text + "'," +
           "MANAGE_TYPE='" + MANAGE_TYPE2.SelectedValue + "',";// +
        if (BEGIN_DATE2.Text != "")
        {
            sqlstr = sqlstr + "BEGIN_DATE='" + BEGIN_DATE2.Text + "',";
        }
        if (USE_DATE2.Text != "")
        {
            sqlstr = sqlstr + "USE_DATE='" + USE_DATE2.Text + "',";
        }

        sqlstr = sqlstr + "PLAN_MONEY='" + PLAN_MONEY2.Text + "'," +
            "SUM_YEARMONEY='" + SUM_YEARMONEY2.Text + "'," +
            "ALL_MONEY='" + ALL_MONEY2.Text + "'," +
            "YUS_MONEY='" + YUS_MONEY2.Text + "'," +
            "ENV_MONEY='" + ENV_MONEY2.Text + "'," +
            "OTHER_SUMMONEY='" + OTHER_SUMMONEY2.Text + "'," +
            "LEND_MONEY='" + LEND_MONEY2.Text + "'," +
            "FOREIN_MONEY='" + FOREIN_MONEY2.Text + "'," +
            "SELF_MONEY='" + SELF_MONEY2.Text + "'," +
            "DESIGN_ABILITY='" + DESIGN_ABILITY2.Text + "'" +
            "where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"] + " and PROJECT_ID='00003'";
        sqlcmd.CommandText = sqlstr;
        sqlcmd.ExecuteNonQuery();

        sqlstr = @"update a01110 set PROJECT_NAME='" + PROJECT_NAME3.Text + "'," +
           "MANAGE_TYPE='" + MANAGE_TYPE3.SelectedValue + "',";// +
        if (BEGIN_DATE3.Text != "")
        {
            sqlstr = sqlstr + "BEGIN_DATE='" + BEGIN_DATE3.Text + "',";
        }
        if (USE_DATE3.Text != "")
        {
            sqlstr = sqlstr + "USE_DATE='" + USE_DATE3.Text + "',";
        }

        sqlstr = sqlstr + "PLAN_MONEY='" + PLAN_MONEY3.Text + "'," +
            "SUM_YEARMONEY='" + SUM_YEARMONEY3.Text + "'," +
            "ALL_MONEY='" + ALL_MONEY3.Text + "'," +
            "YUS_MONEY='" + YUS_MONEY3.Text + "'," +
            "ENV_MONEY='" + ENV_MONEY3.Text + "'," +
            "OTHER_SUMMONEY='" + OTHER_SUMMONEY3.Text + "'," +
            "LEND_MONEY='" + LEND_MONEY3.Text + "'," +
            "FOREIN_MONEY='" + FOREIN_MONEY3.Text + "'," +
            "SELF_MONEY='" + SELF_MONEY3.Text + "'," +
            "DESIGN_ABILITY='" + DESIGN_ABILITY3.Text + "'" +
            "where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"] + " and PROJECT_ID='00004'";
        sqlcmd.CommandText = sqlstr;
        sqlcmd.ExecuteNonQuery();

        sqlstr = @"update a01110 set jy='weijiaoyan' where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
        sqlcmd.CommandText = sqlstr;
        sqlcmd.ExecuteNonQuery();

        sqlstr = @"update a01110 set ALL_MONEY=YUS_MONEY+ENV_MONEY+OTHER_SUMMONEY where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
        sqlcmd.CommandText = sqlstr;
        sqlcmd.ExecuteNonQuery();

        //判断数据是否合规

        string jy = "";
        int yzcw = 0;
        sqlstr = @"select * from a01110  where SUM_YEARMONEY<ALL_MONEY and ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
        sqlcmd.CommandText = sqlstr;
        SqlDataReader reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (reader.Read())
        {

            {

                jy = reader["PROJECT_NAME"].ToString() + " 的6.至上年底累计完成投资必须大于等于上年完成投资及资金来源合计\n";
                yzcw = 1;
            }

        }
        sqlstr = @"select * from a01110  where OTHER_SUMMONEY<LEND_MONEY+FOREIN_MONEY+SELF_MONEY and ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
        sqlcmd.CommandText = sqlstr;
        reader.Close();
        sqlcon.Open();
        SqlDataReader reader1 = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (reader1.Read())
        {

            {

                jy = reader1["PROJECT_NAME"].ToString() + " 的其他资金合计必须大于等于11+12+13\n";
                yzcw = 1;
            }

        }
        TextBox100.Text = "";
        if (yzcw == 0)//BIOLER_CNT
        {

            sqlstr = "update  a01101 set jy='ruanjian' where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
            sqlcmd.CommandText = sqlstr;
            reader1.Close();
            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
            Label5.Text = "更新完成";
        }
        else
        {
            Label5.Text = "更新有错误";
            TextBox100.Text = jy;
        }

    }
    public string DisplayStr()
    {
        YingYongHanShu yyhs = new YingYongHanShu();

        return yyhs.DisplayStr2("101110");
    }

}